package com.mvpdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mvpdemo.bean.User;

import java.util.ArrayList;

public class RvAdapter extends RecyclerView.Adapter<RvAdapter.RvItemHolder> {

    private Context mContext;
    private ArrayList<User> users;

    public RvAdapter(Context mContext, ArrayList<User> users) {
        this.mContext = mContext;
        this.users = users;
    }

    @NonNull
    @Override
    public RvItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new RvItemHolder(LayoutInflater.from(mContext).inflate(R.layout.item_rv_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RvItemHolder holder, int position) {
        String name = "用户名: " + users.get(position).getUserName();
        holder.userName.setText(name);

        String pwd = "账户: " + users.get(position).getUserPwd();
        holder.userPwd.setText(pwd);
    }

    @Override
    public int getItemCount() {
        return users.size();
    }

    class RvItemHolder extends RecyclerView.ViewHolder {

        private TextView userName, userPwd;

        public RvItemHolder(@NonNull View itemView) {
            super(itemView);
            userName = itemView.findViewById(R.id.tv_item_user_name);
            userPwd = itemView.findViewById(R.id.tv_item_user_pwd);
        }
    }
}
